package com.ctos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctos.common.Result;
import com.ctos.dto.SetmealDto;
import com.ctos.entity.Category;
import com.ctos.entity.Dish;
import com.ctos.entity.Setmeal;
import com.ctos.entity.SetmealDish;
import com.ctos.mapper.SetmealMapper;
import com.ctos.service.CategoryService;
import com.ctos.service.DishService;
import com.ctos.service.SetmealDishService;
import com.ctos.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;
    /**
     * 分页显示
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Result<Page<SetmealDto>> setmealPage(Integer page, Integer pageSize, String name) {
        //基础数据page 内不包括菜品信息
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //判断是否传入了名称
        qw.like(name!=null,Setmeal::getName,name);
        //查询排序根据修改时间
        qw.orderByDesc(Setmeal::getUpdateTime);
        this.page(pageInfo,qw);


        //创建dto数据对象 将原有对象属性拷贝到新对象中 但是不拷贝records集合
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, setmealDtoPage,"records");

        //还差存有所有dish对象的records集合 没有拷贝
        //这里获取records集合
        List<Setmeal> records = pageInfo.getRecords();

        //通过stream流遍历 拷贝所有的dish对象到dishdto对象 现在dishdto对象里面没有CategoryName这个属性
        List<SetmealDto> setmealDtos = records.stream().map((setmeal) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            return setmealDto;
        }).collect(Collectors.toList());

        //遍历获得records内所有的dish id属性
        List<Long> categoryIds = records.stream().map((setmeal) -> {
            Long categoryId = setmeal.getCategoryId();
            return categoryId;
        }).collect(Collectors.toList());

        //查询数据库获取所有的菜品分类信息
        List<Category> categorys = categoryService.list();


        //遍历三个集合 所有的菜品信息集合、所有的菜品id集合、所有的基础信息dishdto集合
        for (Long categoryId : categoryIds) {
            for (Category category : categorys) {
                for (SetmealDto setmealDto : setmealDtos) {
                    if (category.getId().equals(categoryId) && setmealDto.getCategoryId().equals(categoryId)){
                        //查找三个集合id都匹配上的菜品 即可赋值完成分类
                        setmealDto.setCategoryName(category.getName());
                    }
                }
            }
        }
        //将集合存入page对象中
        setmealDtoPage.setRecords(setmealDtos);

        return Result.success(setmealDtoPage);
    }

    /**
     * 保存套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveSetmealDto(SetmealDto setmealDto) {
        System.out.println(setmealDto);
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealDto(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);

        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);

        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;

    }

    /**
     * 修改操作
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateDishAndDishFlavor(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setId(null);
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量删除和删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(Long[] ids){
        List<Long> list = Arrays.asList(ids);
        this.removeByIds(list);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Long id : list) {
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);
        }


    }

    /**
     * 修改状态
     * @param status
     * @param ids
     */
    @Override
    public Result<String> modifyStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
            qw.eq(Setmeal::getId,id);
            Setmeal one = this.getOne(qw);
            if (one.getStatus()==0){
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, one.getId());
                List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
                for (SetmealDish setmealDish : list) {
                    LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    dishLambdaQueryWrapper.eq(Dish::getId, setmealDish.getDishId());
                    Dish dish = dishService.getOne(dishLambdaQueryWrapper);
                    if (dish.getStatus()!=1){
                        return Result.error("该套餐内有已被停售的菜品起售失败");
                    }
                }
            }
            LambdaUpdateWrapper<Setmeal> uw = new LambdaUpdateWrapper();
            uw.eq(Setmeal::getId, id);
            uw.set(id!=null, Setmeal::getStatus, status);
            this.update(new Setmeal(),uw);
            return Result.success("success");
        }
       return Result.error("未知错误");
    }

    /**
     * 查询套餐下的菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getSetmealList(Long categoryId) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, 1);
        List<Setmeal> list = this.list(qw);

        // List<SetmealDto> setmealDtos = new ArrayList<>();
        // for (Setmeal setmeal : list) {
        //     SetmealDto setmealDto = new SetmealDto();
        //     BeanUtils.copyProperties(setmeal, setmealDto);
        //     setmealDtos.add(setmealDto);
        // }
        //
        // for (SetmealDto setmealDto : setmealDtos) {
        //     LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //     setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //     List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
        //     setmealDto.setSetmealDishes(setmealDishes);
        // }

        return list;
    }

    /**
     * 获取所有套餐
     * @param setmealId
     * @return
     */
    @Override
    public List<SetmealDish> getSetmealDish(Long setmealId) {
        // LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        // qw.eq(Setmeal::getId, setmealId);
        // Setmeal one = this.getOne(qw);
        //
        // SetmealDto setmealDto = new SetmealDto();
        // BeanUtils.copyProperties(one, setmealDto);
        //
        // LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        // List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
        // setmealDto.setSetmealDishes(setmealDishes);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealId);

        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);

        return list;
    }

}
